﻿/*=========================================================================
 * AUTH: Vincent Breton
 * DESC: Enemies' State Machine
 *=========================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;   //   for Texture2D
using Microsoft.Xna.Framework;  //  for Vector2
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RisingThreat
{
    class EnemyStateMachine
    {
        public enum State { SEARCH, ATTACK };

        public State currentState;

        public Enemy agent { get; set; }

        private const float attackRange = 3.0f;

        public EnemyStateMachine(Enemy a)
        {
            currentState = State.SEARCH;
            agent = a;
        }


        public void update()
        {

            //enemy path finding to team position if someone within attack range switch to attack mode.
            if (currentState == State.SEARCH)
            {
                if (canSeePlayer())
                {
                    currentState = State.ATTACK;
                }
                //if target is long gone from goal position than recalibrate path
                if((agent.pickClosestTarget() - agent.path.goalPosition).Length() > 10.0f)
                        agent.path.currentIndex = -1;

            }
            if (currentState == State.ATTACK)
            {
                if (canSeePlayer() == false)
                {
                    currentState = State.SEARCH;
                    agent.path.currentIndex = -1;
                }
            }

        }

        // just returns a blind distance to test might need to be replaced by a raycast to target to see if it collides (straight-line path)
        public bool canSeePlayer()
        {
            float distance = agent.findDistanceToTarget();

            if (distance < attackRange)
                return true;

            return false;
        }

    }
}
